package io.zhoujie.modules.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhoujie.common.utils.Constant;
import io.zhoujie.common.utils.ObjectUtil;
import io.zhoujie.common.utils.PageUtils;
import io.zhoujie.common.utils.Query;
import io.zhoujie.modules.admin.dao.ActiveDao;
import io.zhoujie.modules.admin.entity.ActiveEntity;
import io.zhoujie.modules.admin.entity.AppUserEntity;
import io.zhoujie.modules.admin.service.ActiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author zhoujie
 * @email 813685764@qq.com
 * @date 2024-02-20 12:10:43
 */
@Service("ActiveService")
@Slf4j
public class ActiveServiceImpl extends ServiceImpl<ActiveDao, ActiveEntity> implements ActiveService {
    @Resource
    private ActiveDao activeDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ActiveEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(ActiveEntity::getAid);
        IPage<ActiveEntity> page = this.page(
                new Query<ActiveEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 查询报名用户
     * @param activeId
     * @return
     */
    @Override
    public List<AppUserEntity> selectUserByActiveId(Integer activeId) {
        if (!ObjectUtil.isEmpty(activeId)) {
            List<AppUserEntity> list = activeDao.selectByActiveId(activeId);
            return list;
        }
        return Collections.emptyList();
    }

    /**
     * 设置状态为结束
     *
     * @param list
     */
    public void deleteByAdmin(List<Long> list) {
        list.forEach(id -> {
//            this.removeById(id);
            //设置活动状态为结束
            this.lambdaUpdate()
                    .set(ActiveEntity::getStatus, Constant.ACTIVE_ED)
                    .eq(ActiveEntity::getAid, id)
                    .update();
        });
    }
}
